package com.cqc.jdk8.Jutil.concurrent.locks.ReentrantLock.Condition;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Counter {
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();

    public void await() {
        lock.lock();
        try {
            System.out.println("await() start..." + Thread.currentThread().getName());
            condition.await();
            System.out.println("await() end..." + Thread.currentThread().getName());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void signal() {
        lock.lock();
        try {
            System.out.println("signal() start..." + Thread.currentThread().getName());
            condition.signal();
            System.out.println("signal() end..." + Thread.currentThread().getName());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void signalAll() {
        lock.lock();
        try {
            System.out.println("signalAll() start..." + Thread.currentThread().getName());
            condition.signalAll();
            System.out.println("signalAll() end..." + Thread.currentThread().getName());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}
